#include "Arm.h"
#include "Player.h"
#include "..\SGD Wrappers\CSGD_TextureManager.h"
#include "Camera.h"
#include "Magnet.h"
//#include "..\SGD Wrappers\CSGD_Direct3D.h"


Arm::Arm(Player* owner, bool isItTheRearArm)
{
	player = owner;
	tM = CSGD_TextureManager::GetInstance();
	ImageID = tM->LoadTexture( "Textures\\Objects\\Arms.png" );
	drawOffsetX	= 17;
	drawOffsetY	= 11;
	toolOffsetX	= 50;
	toolOffsetY	= 25;
	ArmPosX = 0;
	ArmPosY = 0;
	isRear = isItTheRearArm;
	differenceX = player->GetAnimation()->GetCurrAnchorPoint().x - player->GetAnimation()->GetShoulderPoint().x;
	differenceY = player->GetAnimation()->GetCurrAnchorPoint().y - player->GetAnimation()->GetShoulderPoint().y;
	toolpoint.fX = (float)toolOffsetX;
	toolpoint.fY = (float)toolOffsetY;

	looking = LOOKINGSTRAIGHT;
	direction = FACINGRIGHT;

	isFrozen = false;

	myTool = EMPTY;
	pTool = nullptr;
}

Arm::~Arm(void)
{
}

void Arm::Update( float deltaTime )
{
	distancefromhitbox = (int)(player->GetPosX() - differenceX - player->GetHitBox().left);
	ArmPosX = player->GetPosX() - differenceX;
	ArmPosY = player->GetPosY() - differenceY;
	revPosX = player->GetHitBox().right - distancefromhitbox;

	if (!isRear && myTool != MAGNET)
	{
		if( looking == LOOKINGSTRAIGHT && direction == FACINGRIGHT )
		{
			toolpoint.fX = (float)toolOffsetX;
			toolpoint.fY = (float)toolOffsetY;
		}
		else if( looking == LOOKINGSTRAIGHT && direction == FACINGLEFT )
		{
			toolpoint.fX = (float)-toolOffsetX;
			toolpoint.fY = (float)toolOffsetY;
		}
		else if( looking == LOOKINGUP && direction == FACINGRIGHT )
		{
			tVector2D offsetvec = { (float)toolOffsetX, (float)toolOffsetY };
			toolpoint = Vector2DRotate( offsetvec, -D3DX_PI / 2 );
		}	
		else if( looking == LOOKINGUP && direction == FACINGLEFT )
		{
			tVector2D offsetvec = { (float)-toolOffsetX, (float)toolOffsetY };
			toolpoint = Vector2DRotate( offsetvec, D3DX_PI / 2 );
		}	
		else if( looking == LOOKINGDOWN && direction == FACINGRIGHT )
		{
			tVector2D offsetvec = { (float)toolOffsetX, (float)toolOffsetY };
			toolpoint = Vector2DRotate( offsetvec, D3DX_PI / 2 );
		}	
		else if( looking == LOOKINGDOWN && direction == FACINGLEFT )
		{
			tVector2D offsetvec = { (float)-toolOffsetX, (float)toolOffsetY };
			toolpoint = Vector2DRotate( offsetvec, -D3DX_PI / 2 );
		}	
	}
	else if (!isRear && myTool == MAGNET)
	{
		if( looking == LOOKINGSTRAIGHT && direction == FACINGRIGHT )
		{
			toolpoint.fX = (float)toolOffsetX;
			toolpoint.fY = (float)toolOffsetY;
		}
		else if( looking == LOOKINGSTRAIGHT && direction == FACINGLEFT )
		{
			toolpoint.fX = (float)-toolOffsetX;
			toolpoint.fY = (float)toolOffsetY;
		}
		else if( looking == LOOKINGUP && direction == FACINGRIGHT )
		{
			tVector2D offsetvec = { (float)toolOffsetX, (float)toolOffsetY };
			toolpoint = Vector2DRotate( offsetvec, -D3DX_PI / 2 );
		}	
		else if( looking == LOOKINGUP && direction == FACINGLEFT )
		{
			tVector2D offsetvec = { (float)-toolOffsetX, (float)toolOffsetY };
			toolpoint = Vector2DRotate( offsetvec, D3DX_PI / 2 );
		}	
		else if( looking == LOOKINGDOWN && direction == FACINGRIGHT )
		{
			tVector2D offsetvec = { (float)toolOffsetX, (float)toolOffsetY };
			if( dynamic_cast<Magnet*>(pTool)->GetAttched() == nullptr )
				toolpoint = Vector2DRotate( offsetvec, D3DX_PI / 2 );
		}	
		else if( looking == LOOKINGDOWN && direction == FACINGLEFT )
		{
			tVector2D offsetvec = { (float)-toolOffsetX, (float)toolOffsetY };
			if( dynamic_cast<Magnet*>(pTool)->GetAttched() == nullptr )
				toolpoint = Vector2DRotate( offsetvec, -D3DX_PI / 2 );
		}	
	}
	else if(isRear && myTool != MAGNET)
	{
		if( looking == LOOKINGSTRAIGHT && direction == FACINGRIGHT )
		{
			toolpoint.fX = (float)toolOffsetX + 5;
			toolpoint.fY = (float)toolOffsetY - 5;
		}
		else if( looking == LOOKINGSTRAIGHT && direction == FACINGLEFT )
		{
			toolpoint.fX = (float)-toolOffsetX - 5;
			toolpoint.fY = (float)toolOffsetY - 5;
		}
		else if( looking == LOOKINGUP && direction == FACINGRIGHT )
		{
			tVector2D offsetvec = { (float)toolOffsetX, (float)toolOffsetY };
			toolpoint = Vector2DRotate( offsetvec, -D3DX_PI / 2 );
			toolpoint.fX += 5;
			toolpoint.fY -= 5;
		}	
		else if( looking == LOOKINGUP && direction == FACINGLEFT )
		{
			tVector2D offsetvec = { (float)-toolOffsetX, (float)toolOffsetY };
			toolpoint = Vector2DRotate( offsetvec, D3DX_PI / 2 );
			toolpoint.fX -= 5;
			toolpoint.fY -= 5;
		}	
		else if( looking == LOOKINGDOWN && direction == FACINGRIGHT )
		{
			tVector2D offsetvec = { (float)toolOffsetX,(float)toolOffsetY };
			toolpoint = Vector2DRotate( offsetvec, D3DX_PI / 2 );
			toolpoint.fX += 5;
			toolpoint.fY -= 5;
		}	
		else if( looking == LOOKINGDOWN && direction == FACINGLEFT )
		{
			tVector2D offsetvec = { (float)-toolOffsetX, (float)toolOffsetY };
			toolpoint = Vector2DRotate( offsetvec, -D3DX_PI / 2 );
			toolpoint.fX -= 5;
			toolpoint.fY -= 5;
		}
	}
	else if (isRear && myTool == MAGNET)
	{
		if(dynamic_cast<Magnet*>(pTool)->GetAttched() != nullptr)
		{
			if( (looking == LOOKINGSTRAIGHT || looking == LOOKINGDOWN) && direction == FACINGRIGHT )
			{
				toolpoint.fX = (float)toolOffsetX + 5;
				toolpoint.fY = (float)toolOffsetY - 5;
			}
			else if( (looking == LOOKINGSTRAIGHT || looking == LOOKINGDOWN) && direction == FACINGLEFT)
			{
				toolpoint.fX = (float)-toolOffsetX - 5;
				toolpoint.fY = (float)toolOffsetY - 5;
			}
			else if( looking == LOOKINGUP && direction == FACINGRIGHT )
			{
				tVector2D offsetvec = { (float)toolOffsetX, (float)toolOffsetY};
				toolpoint = Vector2DRotate( offsetvec, -D3DX_PI / 2 );
				toolpoint.fX += 5;
				toolpoint.fY -= 5;
			}
			else if( looking == LOOKINGUP && direction == FACINGLEFT )
			{
				tVector2D offsetvec = { (float)-toolOffsetX, (float)toolOffsetY };
				toolpoint = Vector2DRotate( offsetvec, D3DX_PI / 2 );
				toolpoint.fX -= 5;
				toolpoint.fY -= 5;
			}
		}
		else
		{
			if( looking == LOOKINGSTRAIGHT && direction == FACINGRIGHT )
			{
				toolpoint.fX = (float)toolOffsetX + 5;
				toolpoint.fY = (float)toolOffsetY - 5;
			}
			else if( looking == LOOKINGSTRAIGHT && direction == FACINGLEFT )
			{
				toolpoint.fX = (float)-toolOffsetX - 5;
				toolpoint.fY = (float)toolOffsetY - 5;
			}
			else if( looking == LOOKINGUP && direction == FACINGRIGHT )
			{
				tVector2D offsetvec = { (float)toolOffsetX, (float)toolOffsetY };
				toolpoint = Vector2DRotate( offsetvec, -D3DX_PI / 2 );
				toolpoint.fX += 5;
				toolpoint.fY -= 5;
			}	
			else if( looking == LOOKINGUP && direction == FACINGLEFT )
			{
				tVector2D offsetvec = { (float)-toolOffsetX, (float)toolOffsetY };
				toolpoint = Vector2DRotate( offsetvec, D3DX_PI / 2 );
				toolpoint.fX -= 5;
				toolpoint.fY -= 5;
			}	
			else if( looking == LOOKINGDOWN && direction == FACINGRIGHT )
			{
				tVector2D offsetvec = { (float)toolOffsetX, (float)toolOffsetY };
				toolpoint = Vector2DRotate( offsetvec, D3DX_PI / 2 );
				toolpoint.fX += 5;
				toolpoint.fY -= 5;
			}	
			else if( looking == LOOKINGDOWN && direction == FACINGLEFT )
			{
				tVector2D offsetvec = { (float)-toolOffsetX, (float)toolOffsetY };
				toolpoint = Vector2DRotate( offsetvec, -D3DX_PI / 2 );
				toolpoint.fX -= 5;
				toolpoint.fY -= 5;
			}
		}
	}
}

void Arm::Render( void )
{
	if (!isRear)
	{
		if( looking == LOOKINGSTRAIGHT && direction == FACINGRIGHT )
			tM->Draw( ImageID, (int)(ArmPosX - drawOffsetX - Camera::GetInstance()->OffsetX()), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY()) );
		else if( looking == LOOKINGSTRAIGHT && direction == FACINGLEFT )
			tM->Draw( ImageID, (int)(revPosX + drawOffsetX - Camera::GetInstance()->OffsetX()), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY()), -1.0f );
		else if( looking == LOOKINGUP && direction == FACINGRIGHT )
			tM->Draw( ImageID, (int)(ArmPosX - drawOffsetX - Camera::GetInstance()->OffsetX()), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY()), 1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, -D3DX_PI / 2 );
		else if( looking == LOOKINGUP && direction == FACINGLEFT )
			tM->Draw( ImageID, (int)(revPosX + drawOffsetX - Camera::GetInstance()->OffsetX()), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY()), -1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, D3DX_PI / 2 );
		else if( looking == LOOKINGDOWN && direction == FACINGRIGHT )
		{
			if(myTool != MAGNET)
				tM->Draw( ImageID, (int)(ArmPosX - drawOffsetX - Camera::GetInstance()->OffsetX()), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY()), 1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, D3DX_PI / 2 );
			else if(myTool == MAGNET)
			{
				if(dynamic_cast<Magnet*>(pTool)->GetAttched() == nullptr)
					tM->Draw( ImageID, (int)(ArmPosX - drawOffsetX - Camera::GetInstance()->OffsetX()), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY()), 1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, D3DX_PI / 2 );
				else
					tM->Draw( ImageID, (int)(ArmPosX - drawOffsetX - Camera::GetInstance()->OffsetX()), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY()) );
			}
		}
		else if( looking == LOOKINGDOWN && direction == FACINGLEFT )
		{
			if(myTool != MAGNET)
				tM->Draw( ImageID, (int)(revPosX + drawOffsetX - Camera::GetInstance()->OffsetX()), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY()), -1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, -D3DX_PI / 2 );
			else if(myTool == MAGNET)
			{
				if(dynamic_cast<Magnet*>(pTool)->GetAttched() == nullptr)
					tM->Draw( ImageID, (int)(revPosX + drawOffsetX - Camera::GetInstance()->OffsetX()), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY()), -1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, -D3DX_PI / 2 );
				else
					tM->Draw( ImageID, (int)(revPosX + drawOffsetX - Camera::GetInstance()->OffsetX()), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY()), -1.0f );
			}
		}
	}
	else if(isRear)
	{
		if( looking == LOOKINGSTRAIGHT && direction == FACINGRIGHT )
			tM->Draw( ImageID, (int)(ArmPosX - drawOffsetX - Camera::GetInstance()->OffsetX()) + 5, (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY() - 5) );
		else if( looking == LOOKINGSTRAIGHT && direction == FACINGLEFT )
			tM->Draw( ImageID, (int)(revPosX + drawOffsetX - Camera::GetInstance()->OffsetX() - 5), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY()) - 5, -1.0f );
		else if( looking == LOOKINGUP && direction == FACINGRIGHT )
			tM->Draw( ImageID, (int)(ArmPosX - drawOffsetX - Camera::GetInstance()->OffsetX() + 5), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY() - 5), 1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, -D3DX_PI / 2 );
		else if( looking == LOOKINGUP && direction == FACINGLEFT )
			tM->Draw( ImageID, (int)(revPosX + drawOffsetX - Camera::GetInstance()->OffsetX() - 5), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY() - 5), -1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, D3DX_PI / 2 );
		else if( looking == LOOKINGDOWN && direction == FACINGRIGHT )
		{
			if(myTool != MAGNET)
				tM->Draw( ImageID, (int)(ArmPosX - drawOffsetX - Camera::GetInstance()->OffsetX() + 5), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY() - 5), 1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, D3DX_PI / 2 );
			else if(myTool == MAGNET)
			{
				if(dynamic_cast<Magnet*>(pTool)->GetAttched() == nullptr)
					tM->Draw( ImageID, (int)(ArmPosX - drawOffsetX - Camera::GetInstance()->OffsetX() + 5), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY() - 5), 1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, D3DX_PI / 2 );
				else
					tM->Draw( ImageID, (int)(ArmPosX - drawOffsetX - Camera::GetInstance()->OffsetX() + 5), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY() - 5) );
			}
		}
		else if( looking == LOOKINGDOWN && direction == FACINGLEFT )
		{
			if(myTool != MAGNET)
				tM->Draw( ImageID, (int)(revPosX + drawOffsetX - Camera::GetInstance()->OffsetX() - 5), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY() - 5), -1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, -D3DX_PI / 2 );
			else if(myTool == MAGNET)
			{
				if(dynamic_cast<Magnet*>(pTool)->GetAttched() == nullptr)
					tM->Draw( ImageID, (int)(revPosX + drawOffsetX - Camera::GetInstance()->OffsetX() - 5), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY() - 5), -1.0f, 1.0f, 0, (float)drawOffsetX, (float)drawOffsetY, -D3DX_PI / 2 );
				else
					tM->Draw( ImageID, (int)(revPosX + drawOffsetX - Camera::GetInstance()->OffsetX() - 5), (int)(ArmPosY - drawOffsetY - Camera::GetInstance()->OffsetY() - 5), -1.0f );
			}
		}
	}
}

int Arm::GetToolPointX( void )
{
	if( direction == FACINGRIGHT )
		return (int)(ArmPosX + toolpoint.fX);
	else
		return (int)(revPosX + toolpoint.fX);
}

int Arm::GetToolPointY( void )
{
	return (int)(ArmPosY + toolpoint.fY);
}